//
//  TMGFLowGoldRecordViewController.m
//  TMGold
//
//  Created by Mac on 2018/3/17.
//  Copyright © 2018年 tangmu. All rights reserved.
//

#import "TMGFLowGoldRecordViewController.h"
#import "TMGFlowGodlRecrdTableViewCell.h"
#import "TMGFlowGodlRecrdTableViewCell.h"
#import "TMGDealTableViewCell.h"
@interface TMGFLowGoldRecordViewController ()
@property(nonatomic, strong) NSMutableArray                     *dataSouce;
@end

@implementation TMGFLowGoldRecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"交易记录";
    [self configSubView];
    // Do any additional setup after loading the view.
}
#pragma mark --subView
- (void)configSubView{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self sendFlowGoldRequest];
    }];
    [self.tableView.mj_header beginRefreshing];
    
    self.tableView.st_noreslutView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, 100, UIScreenWidth, 300) title:@"暂无交易记录" buttonTitle:@"" buttonHandle:^(NSString *titleString) {
        
    }];
  //  self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count     ;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [TMGDealTableViewCell cellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 5;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    TMGDealTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[TMGDealTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    TMGBuyGoldRecordModel * model = self.dataSouce[indexPath.row];
    cell.model = model;
    if ([model.trade_name containsString:@"卖出"]) {
        cell.moneyLable.text = [NSString stringWithFormat:@"-%@克",model.goods_number];
    }else{
         cell.moneyLable.text = [NSString stringWithFormat:@"+%@克",model.goods_number];
    }
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TMGBuyGoldRecordModel * model = self.dataSouce[indexPath.row];
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark --NetWork Method
- (void)sendFlowGoldRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    [dic setObject:@"2" forKey:@"type"];
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/detail/getTransactionRecord"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 NSArray * array = responseObject[@"data"][@"data"];
                                                 self.dataSouce = [NSMutableArray new];
                                                 for (NSDictionary * modelDic in array) {
                                                     TMGBuyGoldRecordModel * mdoel = [TMGBuyGoldRecordModel mj_objectWithKeyValues:modelDic];
                                                     [self.dataSouce addObject:mdoel];
                                                 }
                                                 [self.tableView reloadData];
                                                 
                                                 [self.tableView.mj_header endRefreshing];
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 [self.tableView.mj_header endRefreshing];
                                             }];
}
@end
